//
//  BaseViewController.m
//  SouthChina
//
//  Created by Mr.Y on 14-10-8.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "BaseViewController.h"
#import "PersonalCenterView.h"
#import "TabbarViewController.h"
#import "TabbarView.h"
#import "EditHeadImgViewController.h"
@interface BaseViewController ()
 

@end

@implementation BaseViewController{
    
    BDKNotifyHUD *hud;

}

@synthesize lableTitle;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void) addHeadImage
{
    NSString *headStr = [DE getGlobalVar:LOGIN_USER_HEADIMG] == nil?@"":[DE getGlobalVar:LOGIN_USER_HEADIMG];
    NSURL *url= [NSURL URLWithString: [headStr hasPrefix:@"http"]?headStr:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,headStr]];

    _btnHeadImageBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, (YNEW), 40, 40)];
    [_btnHeadImageBtn addTarget:self action:@selector(chickHeadImage) forControlEvents:UIControlEventTouchUpInside];
    [_btnHeadImageBtn setTitle:@"" forState:UIControlStateNormal];
    [_btnHeadImageBtn setBackgroundColor:[UIColor clearColor]];

    _imHeadImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40,40)];
    [_imHeadImage setBackgroundColor:[UIColor clearColor]];
    [_imHeadImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    _imHeadImage.layer.masksToBounds = YES;
    _imHeadImage.layer.cornerRadius = 20;
    
    [_btnHeadImageBtn addSubview:_imHeadImage];
    [self.titlebar addSubview:_btnHeadImageBtn];
    
    //增加姓名
    _lablename = [[UILabel alloc] initWithFrame:CGRectMake(55, (YNEW)+15, 60, 15)];
    _lablename.textAlignment = NSTextAlignmentLeft;
    _lablename.textColor = [UIColor whiteColor];
    _lablename.font = [UIFont boldSystemFontOfSize:16.0f];
    _lablename.backgroundColor = [UIColor clearColor];
    _lablename.text = [DE getGlobalVar:LOGIN_USER_NAME] == nil?@"":[DE getGlobalVar:LOGIN_USER_NAME];
    [self.titlebar addSubview:_lablename];
}

- (void)chickHeadImage
{
    //直接跳转到更改头像的页面
    EditHeadImgViewController * editmobileview = [[EditHeadImgViewController alloc]init];
    [kAppDelegate.iCSDrawerController.navigationController pushViewController:editmobileview animated:NO ];
    
}


- (void) addTitleBarWithBackBtn:(NSString *)title needReturn:(bool)need{

    _titlebar= [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TITLE_BAR_HEGIHT+(YNEW))];
    _titlebar.backgroundColor = BASIC_BLUE_COLOR;
    lableTitle = [[UILabel alloc]initWithFrame:CGRectMake(40.0f, YNEW, SCREEN_WIDTH-80, 40)];
    lableTitle.text = title;
    lableTitle.textAlignment = NSTextAlignmentCenter;
    lableTitle.textColor = [UIColor whiteColor];
    lableTitle.backgroundColor = [UIColor clearColor];
    lableTitle.font = [UIFont systemFontOfSize:20.0];
    [_titlebar addSubview:lableTitle];
    
    if(need){
        UIImage *hamburger = [UIImage imageNamed:@"icon_leftArrow_white.png"];
        UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
        btnBack.frame = CGRectMake(0.0f, (YNEW)-5, 60.0f, 50.0f);
        [btnBack setImage:hamburger forState:UIControlStateNormal];
        [btnBack addTarget:self action:@selector(btnBack) forControlEvents:UIControlEventTouchUpInside];
        [_titlebar addSubview:btnBack];
    }
    
    
    [self.view addSubview:_titlebar];
    
}
//设置首页的导航条
- (void) addTitleBarWithBackBtn:(NSString *)title{
    [self addTitleBarWithBackBtn:title needReturn:NO];
}

-(void)refreshImagehead{
    NSString *headStr = [DE getGlobalVar:LOGIN_USER_HEADIMG] == nil?@"":[DE getGlobalVar:LOGIN_USER_HEADIMG];
    NSURL *url= [NSURL URLWithString: [headStr hasPrefix:@"http"]?headStr:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,headStr]];
    [_imHeadImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    
    _lablename.text = [DE getGlobalVar:LOGIN_USER_NAME] == nil?@"":[DE getGlobalVar:LOGIN_USER_NAME];
    [self.titlebar addSubview:_lablename];
}

//返回上一级页面
- (void)btnBack {
    

    if ([self.view viewWithTag:MBProgressHUD_TAG]) {
        NSLog(@"---MB is show....!");
        return;
    }
    
   [kAppDelegate.iCSDrawerController.navigationController popViewControllerAnimated:YES];
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    UISwipeGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [[self view] addGestureRecognizer:recognizer];

    
     UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(btnBack)];
     rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
     [self.view addGestureRecognizer:rightSwipeGestureRecognizer];
}

- (void)viewWillAppear:(BOOL)animated{    // Called when the view is
    NSInteger a =  self.navigationController.viewControllers.count;
    a =  kAppDelegate.iCSDrawerController.navigationController.viewControllers.count;
    //NSLog(@" ----------%@---------------%ld----",self.navigationController,(long)a);
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}



/**
 * 显示提示文字，在指定时间内自动消失
 * text 提示文字
 * duration 显示时间，持续该时间后提示界面消失
 */
-(void)showAlerHUD:(NSString*)text
{
    hud = [[BDKNotifyHUD alloc] initWithImage:[UIImage imageNamed:@"icon_warning"]  text:text];
    hud.center = CGPointMake(self.view.center.x, self.view.center.y);
    [self.view addSubview:hud];
    [hud presentWithDuration:1.5 speed:0.5f inView:self.view completion:^{
        [hud removeFromSuperview];
    }];
}

#pragma mark - close keyboard
- (void)colseKeyboard{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}


-(void)handleSwipeFrom{
    
    [self.view endEditing:YES];
    
}

@end
